package a03_哈希;

import java.util.HashSet;
import java.util.Set;

/**
 * <p>
 * a03_快乐数
 * </p>
 *
 * @author flyduck
 * @since 2024-06-24
 */
public class a04_快乐数 {

    public static void main(String[] args) {
        int n = 19;
        System.out.println(isHappy(n));
    }


    public static boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        while (true) {
            int sum = getNextNumber(n);
            if(sum == 1){
                return true;
            }
            if(set.contains(sum)){
                break;
            }
            set.add(sum);
            n = sum;
        }
        return false;
    }

    //计算平方和
    //129
    //19/10=1 ^ 29
    //29/10=2 ^ 9
    private static int getNextNumber(int n) {
        int sum = 0;
        while (n > 0) {
            sum = sum + ((n%10) * (n%10));
            n = n / 10;
        }
        return sum;
    }


}
